package com.topisv.tms.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.taobao.api.ApiException;
import com.topisv.tms.service.EdiOrderService;
import com.topisv.tms.service.ShopSourceService;

/**
 * 淘宝api工具类
 * @author Administrator
 *
 */
@Component
public class TaoBaoAPITool {
	
	public final String appKey="1023011794";
	private final String appSecret="sandbox529570f1caf3d93919295eafe";
	public final String sendMsgUrl="http://gw.api.taobao.com/router/rest";
	
	@Autowired
	private EdiOrderService  ediOrderService;
	@Autowired
	private ShopSourceService shopSourceService;
	
	
	/**
	 * 回传订单处理状态
	 * @param orderno  订单号
	 * @param status 订单状态
	 * @param sellerNick 商家nick
	 * @param sessionKey  商家授权sessionKey
	 * @return true：成功     false：失败
	 * @throws Exception 
	 * @throws ApiException 
	 */
	public  boolean resendOrderStatus(String orderno,String status) throws Exception {
		try {
			/*String sellerNick; 
			String sessionKey;
			String fromno; //淘宝订单号
			
			//查询订单对应的商家信息
			EdiOrder eOrder=this.ediOrderService.findByOrdeno(orderno);
			if(eOrder==null){
				return false;
			}
			fromno=eOrder.getFromno();

			ShopSource shopSource=this.shopSourceService.findByShopCode(eOrder.getShopCode());
			if(shopSource==null){
				return false;
			}
			sellerNick=shopSource.getTopisvNick();
			sessionKey=shopSource.getTopisvSessionKey();
			if(StringUtils.isBlank(sessionKey) || StringUtils.isBlank(sellerNick) ){
				return false;
			}
			
			//创建淘宝api客户端对象
			TaobaoClient client=new DefaultTaobaoClient(sendMsgUrl, appKey, appSecret);
	    	TmcMessageProduceRequest req=new TmcMessageProduceRequest();
	    	req.setTopic("taobao_jds_TradeTrace ");
	
	        //封装传递参数
	    	Map<String,Object> param=new HashMap< String,Object >();
	    	param.put("tid",Long.valueOf(fromno)); //92245085127175L
	    	param.put("status",status);             //X_SERVICE_AUDITED
	    	param.put("action_time",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); //"2014-03-09 10:10:10"
	    	param.put("operator","派友");
	    	param.put("seller_nick",sellerNick); //"sandbox_b_12"
	    	String content=new JSONWriter().write(param);
	    	req.setContent(content);
	    	
	    	//发送消息，返回结果
	    	TmcMessageProduceResponse response= client.execute(req, sessionKey);
			return response.getIsSuccess();*/
			return true;
		} catch (Exception e) {
			throw e;
		}
    	
	}
	

}
